package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.client.enums.TriggerStrategy;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.CuccServiceOfferConsts;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.FeedateAttrHandler;
import com.iwhalecloud.bss.kite.cucc.service.pos.handler.CuccPosServiceUtils;
import com.iwhalecloud.bss.kite.cucc.service.util.CalculateExpireTimeUtil;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;

@Service
@AttrHandler(FeedateAttrHandler.class)
public class FeedateAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        // 趸交资费联动
        this.oneOffFeeLinkAttr(attr, inst, data);
        // 资费生效时间需要联动失效时间
        if (KiteStringUtils.equals(attr.getFieldName(), LocalKeyConsts.STARTING_DATE_FIELD_NAME)) {
            CalculateExpireTimeUtil.dealExpireTime(attr, inst, data);
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }

    private void oneOffFeeLinkAttr(KiteAttr attr, KiteInst inst, AttrData data) {
        String sceneInstId = data.getSceneInstId();
        List<KiteInst> kiteInsts = CuccPosServiceUtils.queryOneOffFees(sceneInstId);
        if (kiteInsts.isEmpty()) {
            return;
        }
        
        //修改失效时间联动
        if (LocalKeyConsts.END_DATE_FIELD_NAME.equals(attr.getFieldName())) {
            if (CuccPosServiceUtils.isOneOffAddtionalOffer(inst.getSpecId())
                && (KiteStringUtils.equalsAny(inst.getActionType().name(), ActionType.M.name(), ActionType.K.name()) || TriggerStrategy.LOAD_VALUE.equals(data.getTriggerStrategy()))) {
                this.handleDamageFee(sceneInstId, attr, inst);
            }
        }
    }

    private void handleDamageFee(String sceneInstId, KiteAttr attr, KiteInst inst) {
        List<String> serviceOfferIdList = querySceneInstService.getExtParameterValue(sceneInstId, "BUSI_CHANGE_SERVICEOFFERID");//获取页面勾选的操作类型
        KiteSceneInst sceneInst = querySceneInstService.getSceneInst(sceneInstId);
        if ((KiteListUtils.isNotEmpty(serviceOfferIdList) && serviceOfferIdList.contains("1009"))
            || KiteStringUtils.isEqual(CuccServiceOfferConsts.SERVICE_OFFER_ACCOUNT_CANCEL, sceneInst.getServiceOfferId())) {
            //是否收取违约金
            KiteAttr calDamageFeeAttr = inst.getAttr("210004554");
            calDamageFeeAttr.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_D);
            calDamageFeeAttr.setIsNull(LocalKeyConsts.IFTRUE_F);
        }
    }
}
